package com.guo.autoscan.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

import com.guo.autoscan.ssh2.TelnetUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
public class SchedulingConfig
{
    protected static Logger logger = LoggerFactory.getLogger(SchedulingConfig.class);

    @Scheduled(cron="0/10 * * * * ?")
    public void scan()
            throws Exception
    {

        File file = null;
        InputStreamReader read = null;
        try
        {
            String filepath = "/home/test/deviceinfo.txt";

            String encoding = "UTF-8";
            file = new File(filepath);
            if ((file.isFile()) && (file.exists()))
            {
                read = new InputStreamReader(new FileInputStream(file), encoding);


                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null)
                {
                    logger.info(lineTxt);
                    if (StringUtils.isNotBlank(lineTxt))
                    {
                        Pattern sp = Pattern.compile("\\s{1,}");
                        String[] cols = sp.split(lineTxt);
                        String cmd = "display interface Ten-GigabitEthernet " + cols[3] + "/" + cols[4] + "/" + cols[5];

                        String setcmd1 = "interface Ten-GigabitEthernet " + cols[3] + "/" + cols[4] + "/" + cols[5];

                        String setcmd2 = "bandwidth ";


                        sendcmdtelnet3(cols[0], cols[1], cols[2], cmd, setcmd1, setcmd2, cols[6]);
                    }
                }
            }
            else
            {
                logger.info("找不到指定的文件");
            }
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        finally
        {
            if (read != null) {
                read.close();
            }
        }
    }

    private void sendcmdtelnet3(String host, String user, String pwd, String cmd, String setcmd1, String setcmd2, String maxs)
    {
        logger.info(host + ":" + user + ":" + pwd + ":" + cmd + ":" + setcmd1 + ":" + setcmd2);


        String result = sendcmd2(host, user, pwd, cmd);

        logger.info("执行命令为:" + cmd);
        logger.info("返回：" + result);
        if ((StringUtils.isNotBlank(result)) && (!result.contains("^")))
        {
            int start=result.indexOf("Last 120 second output:");
            int end =result.indexOf("Input (total):");
            String substr=result.substring(start,end);
            String temstr=substr.split("bytes/sec")[1].trim();
            String datastr=temstr.split("%")[0].trim();
            logger.info("获得的有用数据为:" + datastr);
            Integer liyonlv = Integer.valueOf(datastr);
            String[] maxlist = maxs.replaceAll("；",";").split(";");
           if (liyonlv.intValue() <=27) {
                  sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[0]);
            } else if ((liyonlv.intValue() >= 28) && (liyonlv.intValue() <=32)) {
                sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[1]);
            } else if ((liyonlv.intValue() >= 33) && (liyonlv.intValue() <=37)) {
                sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[2]);
            } else if ((liyonlv.intValue() >= 38) && (liyonlv.intValue() <= 42)) {
                sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[3]);
            } else if ((liyonlv.intValue() >= 43) && (liyonlv.intValue() <=47)) {
                sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[4]);
            } else if ((liyonlv.intValue() >= 48) && (liyonlv.intValue() <=52)) {
                sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[5]);
            } else if ((liyonlv.intValue() >= 53) && (liyonlv.intValue() <=57)) {
                sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[6]);
            } else if((liyonlv.intValue() >= 58) && (liyonlv.intValue() <=62)){
               sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[7]);
            } else if((liyonlv.intValue() >= 63) && (liyonlv.intValue() <=67)){
               sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[8]);
            } else if((liyonlv.intValue() >= 68) && (liyonlv.intValue() <=72)){
               sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[9]);
            } else if((liyonlv.intValue() >= 73) && (liyonlv.intValue() <=77)){
               sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[10]);
            } else if((liyonlv.intValue() >= 78) && (liyonlv.intValue() <=82)){
               sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[11]);
            } else if((liyonlv.intValue() >= 83) && (liyonlv.intValue() <=87)){
               sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[12]);
            } else{
                sendcmdSet(host, user, pwd, "sys", setcmd1, setcmd2 + maxlist[13]);
            }
       }
        else{
            logger.info("[采集到的数据为空或格式有误..............]");
        }
    }
    private String sendcmd2(String host, String user, String pwd, String cmd)
    {
        logger.info(host + ":" + user + ":" + pwd + ":" + cmd);
        TelnetUtil telnet = null;
        String rString = "";
        try
        {
            telnet = new TelnetUtil(host, 23, user, pwd, "JS-TZ-YC-CR-1");
            logger.info("执行了：" + cmd);
            rString = telnet.sendCommand(cmd, 500);
            logger.info("返回" + rString);
            return rString;
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        finally
        {
            try
            {
                if (telnet != null) {
                    telnet.disConnect();
                }
            }
            catch (IOException e)
            {
                logger.error(e.getMessage(), e);
            }
        }
        return rString;
    }

    private String sendcmdSet(String host, String user, String pwd, String cmd1, String cmd2, String cmd3)
    {
        logger.info(host + ":" + user + ":" + pwd + ":" + cmd1 + ":" + cmd2 + ":" + cmd3);

        TelnetUtil telnet = null;
        String rString = "";
        try
        {
            telnet = new TelnetUtil(host, 23, user, pwd, "JS-TZ-YC-CR-1");
            logger.info("执行了：" + cmd1);
            rString = telnet.sendCommand(cmd1, 200);
            logger.info("返回" + rString);
            logger.info("执行了：" + cmd2);
            rString = telnet.sendCommand(cmd2, 200);
            logger.info("返回" + rString);
            logger.info("执行了：" + cmd3);
            rString = telnet.sendCommand(cmd3, 200);
            logger.info("返回" + rString);
            return rString;
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        finally
        {
            try
            {
                if (telnet != null) {
                    telnet.disConnect();
                }
            }
            catch (IOException e)
            {
                logger.error(e.getMessage(), e);
            }
        }

        return rString;
    }
    private void sendcmdtelnetnoweite(String host, String user, String pwd, String cmd, String setcmd1, String setcmd2, String maxs)
    {
        logger.info(host + ":" + user + ":" + pwd + ":" + cmd + ":" + setcmd1 + ":" + setcmd2);

        TelnetUtil telnet = null;
        try
        {
            telnet = new TelnetUtil(host, 23, user, pwd, "JS-TZ-YC-CR-1");

            String result = telnet.sendCommand(cmd, 200);
            logger.info("执行命令为:" + cmd);
            logger.info("返回：" + result);
            String datastr = result.substring(result.lastIndexOf("%") - 3, result
                    .lastIndexOf("%"));
            logger.info("获得的有用数据为:" + datastr.trim());

            Integer liyonlv = Integer.valueOf(datastr.trim());
            String[] maxlist = maxs.split(";");
            if ((liyonlv.intValue() >= 27) && (liyonlv.intValue() < 37))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                telnet.sendCommand("sys");
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                telnet.sendCommand(setcmd1);
                logger.info("达到了% 执行命令：" + setcmd2 + maxlist[1]);
                telnet.sendCommand(setcmd2 + maxlist[1]);
            }
            else if ((liyonlv.intValue() >= 37) && (liyonlv.intValue() < 47))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                telnet.sendCommand("sys");
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                telnet.sendCommand(setcmd1);
                logger.info("达到了% 执行命令：" + setcmd2 + maxlist[2]);
                telnet.sendCommand(setcmd2 + maxlist[2]);
            }
            else if ((liyonlv.intValue() >= 47) && (liyonlv.intValue() < 57))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                telnet.sendCommand("sys");
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                telnet.sendCommand(setcmd1);
                logger.info("达到了% 执行命令：" + setcmd2 + maxlist[3]);
                telnet.sendCommand(setcmd2 + maxlist[3]);
            }
            else if ((liyonlv.intValue() >= 57) && (liyonlv.intValue() < 67))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                telnet.sendCommand("sys");
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                telnet.sendCommand(setcmd1);
                logger.info("达到了% 执行命令：" + setcmd2 + maxlist[4]);
                telnet.sendCommand(setcmd2 + maxlist[4]);
            }
            else if ((liyonlv.intValue() >= 67) && (liyonlv.intValue() < 77))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                telnet.sendCommand("sys");
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                telnet.sendCommand(setcmd1);
                logger.info("达到了% 执行命令：" + setcmd2 + maxlist[5]);
                telnet.sendCommand(setcmd2 + maxlist[5]);
            }
            else if ((liyonlv.intValue() >= 77) && (liyonlv.intValue() < 87))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                telnet.sendCommand("sys");
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                telnet.sendCommand(setcmd1);
                logger.info("达到了% 执行命令：" + setcmd2 + maxlist[6]);
                telnet.sendCommand(setcmd2 + maxlist[6]);
            }
            else if (liyonlv.intValue() >= 87)
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                telnet.sendCommand("sys");
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                telnet.sendCommand(setcmd1);
                logger.info("达到了% 执行命令：" + setcmd2 + maxlist[7]);
                telnet.sendCommand(setcmd2 + maxlist[7]);
            }
            else if (liyonlv.intValue() < 27)
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                telnet.sendCommand("sys");
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                telnet.sendCommand(setcmd1);
                logger.info("达到了% 执行命令：" + setcmd2 + maxlist[0]);
                telnet.sendCommand(setcmd2 + maxlist[0]);
            }
            return;
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        finally
        {
            try
            {
                if (telnet != null) {
                    telnet.disConnect();
                }
            }
            catch (IOException e)
            {
                logger.error(e.getMessage(), e);
            }
        }
    }

    private void sendcmdtelnet(String host, String user, String pwd, String cmd, String setcmd1, String setcmd2, String maxs)
    {
        logger.info(host + ":" + user + ":" + pwd + ":" + cmd + ":" + setcmd1 + ":" + setcmd2);

        TelnetUtil telnet = null;
        try
        {
            telnet = new TelnetUtil(host, 23, user, pwd, "JS-TZ-YC-CR-1");

            String result = telnet.sendCommand(cmd, 200);
            logger.info("执行命令为:" + cmd);
            logger.info("返回：" + result);
            String datastr = result.substring(result.lastIndexOf("%") - 3, result
                    .lastIndexOf("%"));
            logger.info("获得的有用数据为:" + datastr.trim());

            Integer liyonlv = Integer.valueOf(datastr.trim());
            String[] maxlist = maxs.split(";");
            if ((liyonlv.intValue() >= 27) && (liyonlv.intValue() < 37))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                String mString2 = telnet.sendCommand("sys", 500);
                logger.info("返回" + mString2);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                String mString = telnet.sendCommand(setcmd1, 200);
                logger.info("返回：" + mString);
                logger.info("达到了% 执行命令：" + setcmd2 + maxlist[1]);
                mString = telnet.sendCommand(setcmd2 + maxlist[1], 200);
                logger.info("返回：" + mString);
            }
            else if ((liyonlv.intValue() >= 37) && (liyonlv.intValue() < 47))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                String mString2 = telnet.sendCommand("sys", 500);
                logger.info("返回" + mString2);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                String mString = telnet.sendCommand(setcmd1, 200);
                logger.info("返回：" + mString);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd2 + maxlist[2]);

                mString = telnet.sendCommand(setcmd2 + maxlist[2], 200);
            }
            else if ((liyonlv.intValue() >= 47) && (liyonlv.intValue() < 57))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                String mString2 = telnet.sendCommand("sys", 500);
                logger.info("返回" + mString2);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                String mString = telnet.sendCommand(setcmd1, 200);
                logger.info("返回：" + mString);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd2 + maxlist[3]);

                mString = telnet.sendCommand(setcmd2 + maxlist[3], 200);
            }
            else if ((liyonlv.intValue() >= 57) && (liyonlv.intValue() < 67))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                String mString2 = telnet.sendCommand("sys", 500);
                logger.info("返回" + mString2);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                String mString = telnet.sendCommand(setcmd1, 200);
                logger.info("返回：" + mString);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd2 + maxlist[4]);

                mString = telnet.sendCommand(setcmd2 + maxlist[4], 200);
            }
            else if ((liyonlv.intValue() >= 67) && (liyonlv.intValue() < 77))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                String mString2 = telnet.sendCommand("sys", 500);
                logger.info("返回" + mString2);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                String mString = telnet.sendCommand(setcmd1, 200);
                logger.info("返回：" + mString);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd2 + maxlist[5]);

                mString = telnet.sendCommand(setcmd2 + maxlist[5], 200);
            }
            else if ((liyonlv.intValue() >= 77) && (liyonlv.intValue() < 87))
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                String mString2 = telnet.sendCommand("sys", 500);
                logger.info("返回" + mString2);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                String mString = telnet.sendCommand(setcmd1, 200);
                logger.info("返回：" + mString);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd2 + maxlist[6]);

                mString = telnet.sendCommand(setcmd2 + maxlist[6], 200);
            }
            else if (liyonlv.intValue() >= 87)
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                String mString2 = telnet.sendCommand("sys", 500);
                logger.info("返回" + mString2);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                String mString = telnet.sendCommand(setcmd1, 200);
                logger.info("返回：" + mString);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd2 + maxlist[7]);

                mString = telnet.sendCommand(setcmd2 + maxlist[7], 200);
            }
            else if (liyonlv.intValue() < 27)
            {
                logger.info("达到了" + datastr.trim() + "% 执行命令：sys");
                String mString2 = telnet.sendCommand("sys", 500);
                logger.info("返回" + mString2);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd1);
                String mString = telnet.sendCommand(setcmd1, 200);
                logger.info("返回：" + mString);
                logger.info("达到了" + datastr.trim() + "% 执行命令：" + setcmd2 + maxlist[0]);

                mString = telnet.sendCommand(setcmd2 + maxlist[0], 200);
            }
            return;
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        finally
        {
            try
            {
                if (telnet != null) {
                    telnet.disConnect();
                }
            }
            catch (IOException e)
            {
                logger.error(e.getMessage(), e);
            }
        }
    }

    private void sendcmdtelnet2(String host, String user, String pwd, String cmd, String setcmd1, String setcmd2, String maxs)
    {
        logger.info(host + ":" + user + ":" + pwd + ":" + cmd + ":" + setcmd1 + ":" + setcmd2);

        TelnetUtil telnet = null;
        try
        {
            telnet = new TelnetUtil(host, 23, user, pwd, "JS-TZ-YC-CR-1");
            logger.info("达到了 执行命令：sys");
            String mString2 = telnet.sendCommand("sys", 200);
            logger.info("返回" + mString2);
            logger.info("达到了执行命令：" + setcmd1);
            String mString = telnet.sendCommand(setcmd1, 200);
            logger.info("返回：" + mString);
            logger.info("达到了" + setcmd2);
            mString = telnet.sendCommand(setcmd2 + 3000000, 200);
            logger.info("返回：" + mString); return;
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        finally
        {
            try
            {
                if (telnet != null) {
                    telnet.disConnect();
                }
            }
            catch (IOException e)
            {
                logger.error(e.getMessage(), e);
            }
        }
    }


}
